import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

public class MapDecompressor {
	public static List<Integer> mapIndices = null;
	public static Map<Integer, byte[]> mapBuffer = new HashMap<Integer, byte[]>();

	public static void unpackAll() {

	}

	public static byte[] grabMap(int id) throws IOException {
		if (new File("bin/cache/maps/" + id + ".dat").exists()) {
			System.out.println("Grabbing .dat " + id + ".");
			return readFile("bin/cache/maps/" + id + ".dat");
		}
		if (new File("cache/maps/" + id + ".dat").exists()) {
			System.out.println("Loading map " + id + ".");
			return readFile("cache/maps/" + id + ".dat");
		}
		
		System.out.println("Missing map " + id + ".");
		return null;
		/*if (new File("maps/unpacked/cache/" + id + ".dat").exists()) {
			System.out.println("Grabbing cached " + id + ".dat");
			return readFile("maps/unpacked/cache/" + id + ".dat");
		}*/
		/*if (mapIndices == null)
			loadIndex2();
		if (mapBuffer.get(id) == null) {
			RandomAccessFile raf_cache = new RandomAccessFile(
					"./maps/MAP_CACHE.dat", "rw");
			RandomAccessFile raf_index = new RandomAccessFile(
					"./maps/MAP_CACHE.idx", "rw");
			int pos = getIndexPosition(id);
			if (pos == -1) {
				return null;
			}
			raf_index.seek(pos * 12);
			raf_cache.seek(raf_index.readInt());
			byte[] b = new byte[raf_index.readInt()];
			raf_cache.readFully(b);
			b = inflate(b, raf_index.readInt());
			mapBuffer.put(id, b);
			return mapBuffer.get(id);
		}
		return mapBuffer.get(id);*/
	}

	public static void loadIndex2() throws IOException {
		mapIndices = new ArrayList<Integer>();
		DataInputStream dis = new DataInputStream(new FileInputStream(
				"./maps/MAP_CACHE.idx2"));
		for (int i = 0; i < (int) new File("./maps/MAP_CACHE.idx2").length() / 2; i++) {
			int fileId = (int) dis.readShort();
			mapIndices.add(fileId);
			RandomAccessFile raf_cache = new RandomAccessFile(
					"./maps/MAP_CACHE.dat", "rw");
			RandomAccessFile raf_index = new RandomAccessFile(
					"./maps/MAP_CACHE.idx", "rw");
			raf_index.seek(i * 12);
			raf_cache.seek(raf_index.readInt());
			byte[] b = new byte[raf_index.readInt()];
			raf_cache.readFully(b);
			b = inflate(b, raf_index.readInt());
			save(b, fileId);
		}
	}

	private static void save(byte[] b, int fileId) {
	}

	public static final byte[] readFile(String s) {
		try {
			File f = new File(s);
			int i = (int) f.length();
			byte b[] = new byte[i];
			DataInputStream datainputstream = new DataInputStream(
					new BufferedInputStream(new FileInputStream(s)));
			datainputstream.readFully(b, 0, i);
			datainputstream.close();
			return b;
		} catch (Exception e) {
		}
		return null;
	}

	public static int getIndexPosition(int id) throws IOException {
		if (mapIndices.contains(id)) {
			for (int i = 0; i < mapIndices.size(); i++) {
				if (mapIndices.get(i) == id) {
					return i;
				}
			}
		}
		return -1;
	}

	public static byte[] inflate(byte[] b, int l) throws IOException {
		byte[] buf = new byte[l];
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		DataInputStream dis = new DataInputStream(new GZIPInputStream(bais));
		dis.readFully(buf, 0, buf.length);
		dis.close();
		return buf;
	}
}
